using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Threading;

namespace DailyTransaction
{
    public class GlobalClass
    {
        public static string user;
        public static int role = 1;
        public static bool islogin;
        public static string servername;
        private static string dbname;
        private static string security;
        private static string username;
        private static string pass;
        //added on 22/7/2009
        private static string timeout;
        //end
        private static string connectionstring = "";
        /*public static string connectionStringACC = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\hansos02\\NTPublic\\CMS-Interface\\cms-data.mdb;Jet OLEDB:Database Password=sunpac;";
        public static string connectionStringSUN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\hansos02\\NTPublic\\CMS-Interface\\cms-data-sun.mdb;Jet OLEDB:Database Password=sunpac;";*/
        public static string connectionStringACC = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\hcmsos16\\Public\\CMS-Interface\\cms-data.mdb;Jet OLEDB:Database Password=sunpac;";
        public static string connectionStringSUN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\hcmsos16\\Public\\CMS-Interface\\cms-data-sun.mdb;Jet OLEDB:Database Password=sunpac;";
        /*public static string connectionStringACC = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=P:\\CMS-Interface\\cms-data.mdb;Jet OLEDB:Database Password=sunpac;";
        public static string connectionStringSUN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=P:\\CMS-Interface\\cms-data-sun.mdb;Jet OLEDB:Database Password=sunpac;";*/

        //public static string connectionStringACC = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=P:\\CMS-Interface\\cms-data.mdb;Jet OLEDB:Database Password=sunpac;";
        public static string errmsg = "";
        public static Mutex locker = new Mutex(); 
        public static string getConnectionString()
        {
            errmsg = readConnectionString();
            if (errmsg != "")
                return "";

            connectionstring = "Data source=" + servername
                               + ";Integrated Security=" + security
                               + ";User ID=" + username
                               + ";Password=" + pass
                               + ";Initial Catalog=" + dbname
                               + ";Connect Timeout=" + timeout + ";";
            return connectionstring;
        }
        private static string readConnectionString()
        {
            FileStream fstream;
            //bool flag = true;
            try
            {
                fstream = new FileStream(Directory.GetCurrentDirectory() + "\\appconfig.xml", FileMode.Open, FileAccess.Read);
                //fstream = new FileStream("appconfig.xml", FileMode.Open, FileAccess.Read);                
                readFile(fstream);
            }
            catch (IOException ioe)
            {
                errmsg = ioe.Message;
                //flag = false;
            }
            return errmsg;
        }

        private static void readFile(FileStream fstream)
        {
            XmlTextReader reader = new XmlTextReader(fstream);
            reader.WhitespaceHandling = WhitespaceHandling.None;
            if (fstream.Length != 0)
            {
                reader.ReadStartElement();
                while (!reader.LocalName.Equals("configurationDB"))
                {
                    reader.Read();
                    while (!reader.LocalName.Equals("sqlserver"))
                    {
                        string localName = reader.LocalName;
                        //Move to the data node
                        reader.Read();
                        string data = reader.ReadString();
                        switch (localName)
                        {
                            case "servername":
                                servername = data;
                                break;
                            case "databasename":
                                dbname = data;
                                break;
                            case "integrated_sercurity":
                                security = data;
                                break;
                            case "username":
                                username = data;
                                break;
                            case "password":
                                pass = data;
                                break;
                            case "time_out":
                                timeout = data;
                                break;
                        }
                        reader.Read();
                    }
                    reader.Read();
                }
            }
            reader.Close();
            fstream.Close();
        }
        public static void ErrorLog(string ErrorMsg, string filePath, string pageName, string functionName)
        {                      
            try
            {
                locker.WaitOne();
                StreamWriter sw = new StreamWriter(filePath, true);
                sw.WriteLine(DateTime.Now.ToShortDateString() + ": Error message :" + ErrorMsg + " ; Class/Page Name: " + pageName + " ; Function Name: " + functionName + Environment.NewLine);
                sw.Close();
                locker.ReleaseMutex();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
